package com.bigcreate.dao;

import com.bigcreate.entity.Books;
import com.bigcreate.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BookMapper {

    @Insert("insert into books (bookName,author,publishTime,publisher,price,isbn,quantity,description) values (#{bookName}, #{author},#{publishTime},#{publisher},#{price},#{isbn},#{quantity},#{description})")
    int addBook(Books book);

    @Delete("delete from books where id = #{id}")
    int deleteBookById(int id);

    // 如果id是0，就不更新，应用场景：更新一定是从前端传回来的数据，所以id不会是0
    @Update("update books set bookName=#{bookName},author=#{author},publishTime=#{publishTime},publisher=#{publisher},price=#{price},isbn=#{isbn},quantity=#{quantity},description=#{description} where id=#{id}")
    int updateBook(Books book);

    // 修改数量
    @Update("update books set quantity = #{num} where id=#{id}")
    int updateBookNum(@Param("id") int id, @Param("num") int num);

    @Select("select * from books where id = #{id}")
    Books queryBookById(int id);

    @Select("select * from books")
    List<Books> queryAll();

    // 根据名字查询书籍
    List<Books> queryBookByName(@Param("name") String name);

    @Select("select * from users where name = #{name}")
    User getUser(String name);
}
